/**
 * Copyright:Copyright(c)2014-2017
 * Company:厦门市易致达物联网科技有限公司
 * @version 1.0
 */
package shop.controller.front.partnerapply;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import shop.core.FrontBaseController;
import shop.core.Services;
import shop.core.common.bean.JSONResult;
import shop.core.common.dao.page.PagerModel;
import shop.core.plugins.wxmsg.WxMsgUtil;
import shop.services.front.partnerapply.PartnerapplyService;
import shop.services.front.partnerapply.bean.Partnerapply;
import shop.services.manage.system.bean.User;
import shop.services.manage.system.impl.UserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**   
 * @类名称：PartnerapplyController
 * @创建人：Wzl
 * @创建时间：2018-01-03 下午16:39:49      
 * @版本号：1.0
 * @描述：     
 */
@Controller
@RequestMapping("/front/partnerapply/")
public class PartnerapplyController extends FrontBaseController<Partnerapply> {
    @Resource
    private PartnerapplyService partnerapplyService;
    @Resource
    private UserService userService;
    @Override
    public Services<Partnerapply> getService() {
        return partnerapplyService;
    }

    /**
     * 跳转到加入我们页面
     *
     * @return
     */
    @RequestMapping("toPartnerApply")
    public String toPartnerApply() {
        return "/front/joinUs/joinUs";
    }

    /**
     * 跳转到申请页面
     *
     * @return
     */
    @RequestMapping("toApply")
    public String toApply() {
        return "/front/joinUs/apply";
    }

    /**
     * 跳转到选择店铺页面
     *
     * @return
     */
    @RequestMapping("toStoreList")
    public String toStoreList() {
        return "/front/partnerapply/storeList";
    }

    /**
     * 跳转到股东协议页面
     *
     * @return
     */
    @RequestMapping("toAgreement")
    public String toAgreement() {
        return "/front/partnerapply/agreement";
    }

    /**
     * 跳转到提示转账页面
     *
     * @return
     */
    @RequestMapping("toNotice")
    public String toNotice() {
        return "/front/partnerapply/notice";
    }

    /**
     * 查询入股店铺记录
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("selectApplyList")
    @ResponseBody
    public JSONResult selectApplyList(HttpSession session, Partnerapply partnerapply){
        User user=(User)session.getAttribute("userInfo");
        partnerapply.setUserID(user.getId());
        List<Partnerapply> applyList = partnerapplyService.selectApplyList(partnerapply);
        JSONResult jsonResult = new JSONResult();
        jsonResult.setData(applyList);
        return jsonResult;
    }

    /**
     * 查询入伙店铺记录CS
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("selectApplyListCS")
    @ResponseBody
    public JSONResult selectApplyListCS(HttpServletRequest request, Partnerapply partnerapply){
//        User user=(User)session.getAttribute("userInfo");
        User user = new User();                                            //new user
        user.setId(request.getParameter("userID"));                            //获取用户名
        user=userService.selectOne(user);
        partnerapply.setUserID(user.getId());
        partnerapply.setStatus("pass");
        List<Partnerapply> applyList = partnerapplyService.selectApplyList(partnerapply);
        JSONResult jsonResult = new JSONResult();
        jsonResult.setData(applyList);
        return jsonResult;
    }

    /**
     * 股东申请
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "insertPartnerApply", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult insertPartnerApply(HttpSession session,Partnerapply partnerApply){
        JSONResult jsonResult = new JSONResult();
        User user=(User)session.getAttribute("userInfo");
        if(!"0".equals(user.getPid())){
            User subUser=new User();
            subUser.setId(user.getPid());
            subUser=userService.selectOne(subUser);
            partnerApply.setParentName(subUser.getNickname());
        }
        partnerApply.setUserID(user.getId());
        partnerApply.setStatus("init");
        partnerApply.setType("front");
        Partnerapply partnerApplyInfo=partnerapplyService.selectOne(partnerApply);
        if(partnerApplyInfo==null){
            int id =partnerapplyService.insert(partnerApply);
            Partnerapply partnerApplyOne=new Partnerapply();
            partnerApplyOne.setId(String.valueOf(id));
            partnerApplyOne=partnerapplyService.selectOne(partnerApplyOne);
            //股东申请消息通知
            Map<String, String> msgParam = new HashMap<>();
            msgParam.put("first", "您好，您的申请已提交！");
            msgParam.put("keyword1", partnerApplyOne.getApplicant());
            msgParam.put("keyword2", partnerApplyOne.getCreateTime());
            msgParam.put("remark", "请耐心等待审批结果！");
            WxMsgUtil.sendPartnerApplyStatusMsg(msgParam, systemManager, user.getOpenID());
            jsonResult.setMessage("申请成功，请耐心等待管理员审核！");
            return jsonResult;
        }else{
            jsonResult.setSuccess(false);
            jsonResult.setMessage("您已提交过申请，请耐心等待审核结果！");
            return jsonResult;
        }
    }

    @Override
    public JSONResult deleteJson(HttpServletRequest request, Partnerapply partnerapply) throws Exception {
        return null;
    }
}
